package com.unitedvision.persistence.domain;

import java.time.Month;

import javax.persistence.Column;
import javax.persistence.Embeddable;

import com.unitedvision.core.domain.Periode;

@Embeddable
public class PeriodeValue {
	private int year;
	private Month month;
	
	public PeriodeValue() {
		super();
	}

	public PeriodeValue(int year, Month month) {
		super();
		this.year = year;
		this.month = month;
	}
	
	public PeriodeValue(Periode periode) {
		super();
		year = periode.getYear();
		month = periode.getMonth();
	}

	@Column (name = "year")
	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	@Column (name = "month")
	public Month getMonth() {
		return month;
	}

	public void setMonth(Month month) {
		this.month = month;
	}
	
	public Periode toPeriode() {
		return new Periode(year, month);
	}
	
	public static PeriodeValue create(int year, Month month) {
		return new PeriodeValue(year, month);
	}
	
	public static PeriodeValue create(Periode periode) {
		return new PeriodeValue(periode);
	}
	
	/**
	 * Compare this object with the specified one. It results negative, zero, or positive as this object less than, same, or greater than the specified object.
	 * @param periode
	 * @return
	 */
	public int compareTo(PeriodeValue periode) {
		return toPeriode().compareTo(periode.toPeriode());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((month == null) ? 0 : month.hashCode());
		result = prime * result + year;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		
		PeriodeValue other = (PeriodeValue) obj;
		
		if (month != other.month)
			return false;
		if (year != other.year)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "PeriodeValue [year=" + year + ", month=" + month + "]";
	}
}
